package com.ctcemti.mdm.project.common.excelverifyhandler;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.ctcemti.mdm.project.sys.dao.InfDictItemMapper;
import com.ctcemti.mdm.project.sys.dao.OrgPersonMapper;
import com.ctcemti.mdm.project.sys.entity.InfDictItem;
import com.ctcemti.mdm.project.sys.entity.OrgPerson;
import tk.mybatis.mapper.util.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExcelVerifyHandlerToOrgPerson implements IExcelVerifyHandler<OrgPerson> {

    private List<String> listCode;

    private OrgPersonMapper orgPersonMapper;
    private Map<String, String> userPositionMap = new HashMap<>();
    private Map<String, String> userSexMap = new HashMap<>();
    private Map<String, String> userTypeMap = new HashMap<>();
    private Map<String, String> userStateMap = new HashMap<>();

    private InfDictItemMapper infDictItemMapper;

    @Override
    public ExcelVerifyHandlerResult verifyHandler(OrgPerson orgPerson) {

        ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult();

        if(listCode!=null && listCode.size()>0){
            if(listCode.contains(orgPerson.getLoginName())){
                result.setMsg("该登录名表格中已存在");
                result.setSuccess(false);
                return result;
            }
        }
        //职务验证
        if(userPositionMap.get(orgPerson.getTitle())!=null){
            orgPerson.setTitle(userPositionMap.get(orgPerson.getTitle()));
        }else{
            result.setMsg("系统中不存在该职位");
            result.setSuccess(false);
            return result;
        }
        //性别验证
        if(userSexMap.get(orgPerson.getSexy())!=null){
            orgPerson.setSexy(userSexMap.get(orgPerson.getSexy()));
        }else{
            result.setMsg("系统中不存在该性别");
            result.setSuccess(false);
            return result;
        }
        //用户类型验证
        if(userTypeMap.get(orgPerson.getType())!=null){
            orgPerson.setType(userTypeMap.get(orgPerson.getType()));
        }else{
            result.setMsg("系统中不存在该用户类型");
            result.setSuccess(false);
            return result;
        }
        //用户状态验证
        if(userStateMap.get(orgPerson.getState())!=null){
            orgPerson.setState(userStateMap.get(orgPerson.getState()));
        }else{
            result.setMsg("系统中不存在该用户状态");
            result.setSuccess(false);
            return result;
        }
        OrgPerson entity = new OrgPerson();
        entity.setLoginName(orgPerson.getLoginName());
        List<OrgPerson> list =  orgPersonMapper.select(entity);
        if(!StringUtil.isEmpty(orgPerson.getLoginName()) && list.size()>0){
            result.setMsg("该登录名数据库中已存在");
            result.setSuccess(false);
            return result;
        }else{
            listCode.add(orgPerson.getLoginName());
        }
        result.setSuccess(true);
        return result;
    }

    public ExcelVerifyHandlerToOrgPerson(Object object){
        this.listCode = new ArrayList<String>();
        this.orgPersonMapper = (OrgPersonMapper) object;
    }
    public ExcelVerifyHandlerToOrgPerson(Object object,Object object1){
        this.listCode = new ArrayList<String>();
        this.orgPersonMapper = (OrgPersonMapper) object;
        this.infDictItemMapper = (InfDictItemMapper) object1;

        List<InfDictItem> userPosition = infDictItemMapper.selectInfdictitemByInfdictsortCode("UserPosition");
        List<InfDictItem> userSex =infDictItemMapper.selectInfdictitemByInfdictsortCode("UserSex");
        List<InfDictItem> userType =infDictItemMapper.selectInfdictitemByInfdictsortCode("UserType");
        List<InfDictItem> userState =infDictItemMapper.selectInfdictitemByInfdictsortCode("UserState");
        for (InfDictItem tmp : userPosition) {
            userPositionMap.put(tmp.getName(), tmp.getCode());
        }
        for (InfDictItem tmp : userSex) {
            userSexMap.put(tmp.getName(), tmp.getCode());
        }
        for (InfDictItem tmp : userType) {
            userTypeMap.put(tmp.getName(), tmp.getCode());
        }
        for (InfDictItem tmp : userState) {
            userStateMap.put(tmp.getName(), tmp.getCode());
        }
    }
}
